var member;
//存放所有的地址信息
var regionList;
var cityList;
var countyList;
$(function(){	
	
	//获取cookie中的对象
	member=getEntity("memberInfo");
	if(member){
		$("#mId").val(member.mId);
	}else{
		window.location.href=rootPath+"/";
	}

	//获取用户收货地址列表
	memberAddressList();
	 //管理收货地址页
	$('.addr_same .select_addr .clearfix span').click(function(){
			$(this).addClass('selected').siblings().removeClass('selected');
			//$(this).find('img').css('display','block').end().siblings().find('img').css('display','block');
			$(this).find('img').css('display','block').end().siblings().find('img').css('display','none');
			//alert($(this).find('#hiddenMaId').val());
			$("#addressId").val($(this).find('#hiddenMaId').val());
			//alert($("#maId").val());
		})
	//添加常用地址
	$("#confirmAddAddress").click(function(){
        debugger;
		if(!$("#maId")){
			return false;
		}
		var myforms=document.forms;
		var myemail=myforms[0].postcode.value;
		var myReg=/^[1-9][0-9]{5}$/;
		
		if(!myReg.test(myemail)){
			layer.msg("邮编格式不对!",{offset: ['40%', '40%'],icon: 2,time:10000});
		    return false;
		}
		var street = $("#street").val();
		if(!street){
			layer.msg("请输入详细地址",{offset: ['40%', '40%'],icon: 2,time:10000});
			return false;
		}
		if(!$("#name").val()){
			layer.msg("请输入收货人姓名",{offset: ['40%', '40%'],icon: 2,time:10000});
			return false;
		}
		var myforms=document.forms;
		var myphone=myforms[0].tel.value;
	    if(!(/^1(3|4|5|7|8)\d{9}$/.test(myphone))){
				layer.msg("请输入正确的手机号！",{offset: ['40%', '40%'],icon: 2,time:10000});
				return false;
		}
	    if($("#J_SetDefault").is(":checked")){
	    	$("#J_SetDefault").val("1");
	    }else{
	    	$("#J_SetDefault").val("0");
	    }
		var data = $("form").serialize();
		$.ajax({
			type:"post",
			url:rootPath+"/address/addOrUpdateAddress",
			dataType:"JSON",
			data:data,
			success:function(data){
				if(data.resultSuccess){
					$("#addAdress").hide();
					$("#addressList").empty();
					//刷新用户收货地址列表
					memberAddressList();
				}
			}
		});
	});

	//获取并分级展示全国的所有地址信息
	$.ajax({
		type:"get",
		url:rootPath+"/address/selectAllRegion",
		dataType:"JSON",
		async: false,
		success:function(data){
			regionList=data.data;
			$.each(regionList,function(i,item){
				$("#provinceCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			var regionCode=$("#provinceCode").val();
			cityList=regionList[0].city;
			$("#countyCode").empty();
			$.each(cityList,function(i,item){
				$("#cityCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			countyList=cityList[0].area;
			$("#countyCode").empty();
			$.each(countyList,function(i,item){
				$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			$("#province ").val($("#provinceCode").find("option:selected").text());
			$("#city ").val($("#cityCode").find("option:selected").text());
			$("#county").val($("#countyCode").find("option:selected").text());
		}
	})
	//实现省市县的三级联动
	$("#provinceCode").change(function(){
		var regionCode=$("#provinceCode").val();
		$.each(regionList,function(i,item){
			if(regionCode==item.regionCode){
				cityList=item.city;
			}
		})
		$("#cityCode").empty();
		$("#countyCode").empty();
		$.each(cityList,function(i,item){
			$("#cityCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
		})
		countyList=cityList[0].area;
		$.each(countyList,function(i,item){
			$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
		})
		$("#province ").val($("#provinceCode").find("option:selected").text());
		$("#city ").val($("#cityCode").find("option:selected").text());
		$("#county").val($("#countyCode").find("option:selected").text());
	})
	
	$("#cityCode").change(function(){
		var regionCode=$("#cityCode").val();
		$.each(cityList,function(i,item){
			if(regionCode==item.regionCode){
				countyList=item.area;
			}
		})
		$("#countyCode").empty();
		$.each(countyList,function(i,item){
			$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
		})
		$("#province ").val($("#provinceCode").find("option:selected").text());
		$("#city ").val($("#cityCode").find("option:selected").text());
		$("#county").val($("#countyCode").find("option:selected").text());
	});
	$("#countyCode").change(function(){
		$("#county").val($("#countyCode").find("option:selected").text());
	});
});
//修改常用地址
function selectAddressById(maId){
	$("#addAdress").show();
	$.ajax({
		type:'post',
		url:rootPath+"/address/selectAddressById?maId="+maId,
		dataType:"JSON",
		success:function(data){
			var address=data.data;
			$("#postcode").val(address.postcode);
			$("#name").val(address.name);
			$("#street").val(address.street);
			$("#maId").val(address.maId);
			$("#tel").val(address.tel);
			$("#provinceCode option[value="+address.provinceCode+"]").attr("selected",true);
			var regionCode=$("#provinceCode").val();
			$.each(regionList,function(i,item){
				if(regionCode==item.regionCode){
					cityList=item.city;
				}
			})
			$("#cityCode").empty();
			$("#countyCode").empty();
			$.each(cityList,function(i,item){
				$("#cityCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			$("#cityCode option[value="+address.cityCode+"]").attr("selected",true);
			var regionCode=$("#cityCode").val();
			$.each(cityList,function(i,item){
				if(regionCode==item.regionCode){
					countyList=item.area;
				}
			})
			$("#countyCode").empty();
			$.each(countyList,function(i,item){
				$("#countyCode").append("<option value='"+item.regionCode+"'>"+item.name+"</option>")
			})
			$("#countyCode option[value="+address.countyCode+"]").attr("selected",true);
			$("#province ").val(address.province);
			$("#city ").val(address.city);
			$("#county").val(address.county);
			
			if(address.isdefault=='1'){
				$("#J_SetDefault").attr("checked","checked");
				$("#J_SetDefault").val("1");
			}else{
				$("#J_SetDefault").removeAttr("checked");
				$("#J_SetDefault").val("0");
			}
		}
	});
}
//点击添加常用地址时弹出表单
function showForm(){
	if($("#addAdress").css("display")== "none") {
		$("#addAdress").show();
	}else if($("#addAdress").css("display")== "block") {
		$("#addAdress").hide();
	}
};
//删除常用地址
/*function deleteAddressById(maId){
	$.ajax({
		type:'post',
		url:"/address/deleteAddressById?maId="+maId,
		dataType:"JSON",
		success:function(data){
			if(data.resultSuccess){
				location.reload(true);
			}
		}
	})
}*/

//展示用户的常用地址
function memberAddressList(){
	$.ajax({
		type:"get",
		url:rootPath+"/address/selectBymId?mId="+member.mId,
		dataType:"JSON",
		async: false,
		success:function(data){
		    /*<span class='six'>
				<p class='f18 three name'>小希</p>
				<p>河南省郑州市郑东新区</p>
				<p>河南省郑州市郑东新区 457100</p>
				<p>18336396223</p>
				<a href=''>编辑</a>
			</span>*/
			var addressList=data.data;
			$.each(addressList,function(i,item){
				var address="";		
				var selected ="";
				var display="display:none";
				if(i == 0){
					selected ="selected";
					display ="display:block";
					$("#addressId").val(item.maId);
				}
				var postcode =item.postcode?item.postcode:"无";
				
				
				address="<span class='six " + selected+
						"'>"
					+"<p class='f18 three name'>"+item.name+"<a href='javascript:selectAddressById(\""+item.maId+"\");'>编辑</a></p>"
					+"<p class='detailAddress' title='"+item.streetAll+"'>"+item.streetAll+"</p>"
					+"<p>手机："+item.tel+"</p>"		
					+"<p>邮编："+postcode+"</p>"			
					+"<img src='"+rootPath+"/images/xz.png' alt='' style='" + display+
							"' />"
					+"<input type='hidden' id='hiddenMaId' value='"+item.maId+"' />"
					+"</span>";
				$("#addressList").append(address);
			});
			
			var detailAddress = $('.detailAddress').text().substr(0,31)+'...';
			$('.wordO').text(detailAddress);
		}
	});
}
function cancle(){
	$("#form").hidden();
}